import matplotlib.pyplot as plt
from PIL import Image
from keras.preprocessing import image
import glob
import numpy as np


def print_result(path):
    name_list = glob.glob(path)
    fig = plt.figure(figsize=(12, 16))  # 图像大小
    for i in range(3):
        img = Image.open(name_list[i])
        sub_img = fig.add_subplot(131 + i)  # 子图
        sub_img.imshow(img)
    return None




if __name__ == "__main__":
    # 1.定义文件路径
    img_path = '../resources/p02_deep_learning_tensorFlow/image_transformation/in/*'
    in_path = '../resources/p02_deep_learning_tensorFlow/image_transformation/'
    out_path = '../resources/p02_deep_learning_tensorFlow/out/'
    name_list = glob.glob(img_path)  # 根据正则路径拿到所有图像



    # 2.将所有图片指定相同大小
    datagen2 = image.ImageDataGenerator()
    # 输入路径，只用1个文件夹，不训练，保存路径，图片前缀，目标大小224*224
    gen_data2 = datagen2.flow_from_directory(in_path, batch_size=1, shuffle=False,
                                             save_to_dir=out_path + 'resize',  # 需要自己建 rotation_range 文件夹
                                             save_prefix='gen', target_size=(224, 224))
    # 执行变换操作
    for i in range(3):
        gen_data2.next()



    # 3. 旋转角度
    # 先统一大小
    gen = image.ImageDataGenerator()
    data = gen.flow_from_directory(in_path, batch_size=1, shuffle=True,
                                     class_mode=None, target_size=(224, 224))
    np_data = np.concatenate([data.next() for i in range(data.n)]) # 组合数据
    # 再旋转
    datagen3 = image.ImageDataGenerator(rotation_range=45)  # 旋转45度
    datagen3.fit(np_data)
    gen_data3 = datagen3.flow_from_directory(in_path, batch_size=1, shuffle=False,
                                             save_to_dir=out_path + 'rotation_range',
                                             save_prefix='gen', target_size=(224, 224))
    # 执行变换操作
    for i in range(3):
        gen_data3.next()



    # 4. 平移变换
    # 先统一大小
    # 再平移
    # 填充方法 fill_mode
    #    constant: kkkkkkkk|abcd|kkkkkkkk（cval=k）
    #    nearest:  aaaaaaaa|abcd|dddddddd  最近点填充
    #    reflect:  abcddcba|abcd|dbcaabcd
    #    wrap:     abcdabcd|abcd|abcdabcd
    datagen4 = image.ImageDataGenerator(fill_mode='wrap',width_shift_range=0.3, height_shift_range=0.3)  # 相对长度的比例值（可正可负）
    datagen4.fit(np_data)
    gen_data4 = datagen4.flow_from_directory(in_path, batch_size=1, shuffle=False,
                                             save_to_dir=out_path + 'shift',
                                             save_prefix='gen', target_size=(224, 224))
    # 执行变换操作
    for i in range(3):
        gen_data4.next()



    # 5.缩放
    # 先统一大小
    # 再缩放
    datagen5 = image.ImageDataGenerator(zoom_range=0.5)  # 0-1 放大，大于1缩小
    datagen5.fit(np_data)
    gen_data5 = datagen5.flow_from_directory(in_path, batch_size=1, shuffle=False,
                                             save_to_dir=out_path + 'zoom',
                                             save_prefix='gen', target_size=(224, 224))
    # 执行变换操作
    for i in range(3):
        gen_data5.next()



    # 6. 颜色通道
    # 先统一大小
    # 再修改
    datagen6 = image.ImageDataGenerator(channel_shift_range=15)
    datagen6.fit(np_data)
    gen_data6 = datagen6.flow_from_directory(in_path, batch_size=1, shuffle=False,
                                             save_to_dir=out_path + 'channel',
                                             save_prefix='gen', target_size=(224, 224))
    # 执行变换操作
    for i in range(3):
        gen_data6.next()


    # 7.翻转
    # 先统一大小
    # 再翻转
    datagen7 = image.ImageDataGenerator(horizontal_flip=True)
    datagen7.fit(np_data)
    gen_data7 = datagen7.flow_from_directory(in_path, batch_size=1, shuffle=False,
                                             save_to_dir=out_path + 'horizontal',
                                             save_prefix='gen', target_size=(224, 224))
    # 执行变换操作
    for i in range(3):
        gen_data7.next()


    # 8.归一化
    # 先统一大小
    # 再翻转
    datagen8 = image.ImageDataGenerator(rescale=1/255)
    datagen8.fit(np_data)
    gen_data8 = datagen8.flow_from_directory(in_path, batch_size=1, shuffle=False,
                                             save_to_dir=out_path + 'rescale',
                                             save_prefix='gen', target_size=(224, 224))
    # 执行变换操作
    for i in range(3):
        gen_data8.next()